package com.main.hotel.service.impl;

import com.main.hotel.entity.Customer;
import com.main.hotel.entity.OrderInfo;
import com.main.hotel.repository.CustomerRepository;
import com.main.hotel.service.CustomerService;
import com.main.hotel.service.OrderInfoService;
import com.main.hotel.service.RoomService;
import com.main.hotel.util.OrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class CustomerServiceImpl implements CustomerService {
  @Autowired
  private CustomerRepository customerRepository;
  @Autowired
  private RoomService roomService;
  @Override
  public Customer getCustomerById(Integer id) {
    System.out.println(id);
   return customerRepository.getCustomerById(id);
  }

  @Override
  @Transactional
  public int addCustomer(Customer customer) {
    customerRepository.save(customer);
    return 1;
  }

  @Override
  public Customer confirmCustomer(String phone, String password) {
    Customer customer=customerRepository.confirmCustomer(phone,password);
    return customer;
  }

  @Override
  @Transactional
  public int addOrder(OrderInfo orderInfo) {

    Customer customer=customerRepository.getCustomerById(orderInfo.getFoo());
    System.out.println(customer.toString());
    int count=roomService.findRoomCount(orderInfo.getRoomType());
    if(count<1)
    {
      return 0;
    }
    else {
      int r=roomService.book(orderInfo.getRoomType(),orderInfo.getOrder_id());
      if(r==1) {
        List<OrderInfo> orders = new ArrayList<>();
        orderInfo.setRoom_id(roomService.findRoomId(orderInfo.getOrder_id()));
        orderInfo.setCustomer(customer);
        orders.add(orderInfo);
        customer.setOrders(orders);
        return 1;
      }
      else
        return 0;
    }
  }
  @Autowired
  private OrderInfoService orderInfoService;
  public int cancelOrder(String order_id)
  {
    int r1=roomService.cancelBook(order_id);
    System.out.println(r1+"  8888");
    int r2=orderInfoService.cancelOrder(order_id);
   if(r1==1&&r2==1)
   {
     return 1;
   }
   else
     return 0;
  }
}
